use strict;
use warnings;

my $f = "NTDBNEW_Imputation_Train_LD.txt";
open (F, $f) || die "Could not open $f: $!\n";
my @f = <F>;
close F;
my $nLines_train = @f;

print "Number of train lines = $nLines_train\n";

$f = "NTDBNEW_Imputation_Test_LD.txt";
open (F, $f) || die "Could not open $f: $!\n";
@f = <F>;
close F;
my $nLines_test = @f;
print "Number of test lines = $nLines_test\n";

my @ALL = ("NTDBNEW_Imputation_HD_imputed_0.txt",
             "NTDBNEW_Imputation_HD_imputed_1.txt",
             "NTDBNEW_Imputation_HD_imputed_2.txt",
             "NTDBNEW_Imputation_HD_imputed_3.txt",
             "NTDBNEW_Imputation_HD_imputed_4.txt",
             "NTDBNEW_Imputation_LD_imputed_0.txt",
             "NTDBNEW_Imputation_LD_imputed_1.txt",
             "NTDBNEW_Imputation_LD_imputed_2.txt",
             "NTDBNEW_Imputation_LD_imputed_3.txt",
             "NTDBNEW_Imputation_LD_imputed_4.txt");

my @TRAIN = ("NTDBNEW_Imputation_Train_HD_imputed_0.txt",
               "NTDBNEW_Imputation_Train_HD_imputed_1.txt",
               "NTDBNEW_Imputation_Train_HD_imputed_2.txt",
               "NTDBNEW_Imputation_Train_HD_imputed_3.txt",
               "NTDBNEW_Imputation_Train_HD_imputed_4.txt",
               "NTDBNEW_Imputation_Train_LD_imputed_0.txt",
               "NTDBNEW_Imputation_Train_LD_imputed_1.txt",
               "NTDBNEW_Imputation_Train_LD_imputed_2.txt",
               "NTDBNEW_Imputation_Train_LD_imputed_3.txt",
               "NTDBNEW_Imputation_Train_LD_imputed_4.txt");

my @TEST = ("NTDBNEW_Imputation_Test_HD_imputed_0.txt",
              "NTDBNEW_Imputation_Test_HD_imputed_1.txt",
              "NTDBNEW_Imputation_Test_HD_imputed_2.txt",
              "NTDBNEW_Imputation_Test_HD_imputed_3.txt",
              "NTDBNEW_Imputation_Test_HD_imputed_4.txt",
              "NTDBNEW_Imputation_Test_LD_imputed_0.txt",
              "NTDBNEW_Imputation_Test_LD_imputed_1.txt",
              "NTDBNEW_Imputation_Test_LD_imputed_2.txt",
              "NTDBNEW_Imputation_Test_LD_imputed_3.txt",
              "NTDBNEW_Imputation_Test_LD_imputed_4.txt");

for(my $i = 0; $i < @ALL; $i++){
    my $thisALL = $ALL[$i];
    my $thisTRAIN = $TRAIN[$i];
    my $thisTEST = $TEST[$i];
    print "$thisALL\n";
    
    open(my $allFile, "< $thisALL");
    open(my $trainFile, "> $thisTRAIN");
    open(my $testFile, "> $thisTEST");
    my $lineno = 1;
    while(<$allFile>){
        my $line = $_;
        if($lineno <= $nLines_train){
            print $trainFile $line;
        }
        else{
            print $testFile $line;
        }
        $lineno++;
    }
    close($allFile);
    close($trainFile);
    close($testFile);
}
